# -*- coding: utf-8 -*-
from django.shortcuts import render_to_response
from django.http import HttpResponse
from django.template import RequestContext
from quiz.models import Quiz, Histry
import random


def home(request):
    return render_to_response('home.html', {

    }, context_instance=RequestContext(request))

def start(request):
    return render_to_response('start.html', {

    }, context_instance=RequestContext(request))

def question(request):
    if request.method == 'POST':
        # 問題の取得
        record_count = Quiz.objects.count()
        get_id = random.randint(1, record_count)
        record = Quiz.objects.get(pk=get_id)
        # -- 挑戦者名
        challenger = request.POST.get('challenger', '')
        # -- 何問目か
        question_count = int(request.POST.get('question_count', ''))
        question_count = question_count + 1
        # -- 正解数
        correct_count = request.POST.get('correct_count', '')
        # -- 問題のID
        question_id = record.id
        # -- 表示する問題と解答群
        question = record.question
        answer01 = record.answer01
        answer02 = record.answer02
        answer03 = record.answer03

        return render_to_response('question.html', {
            # -- チャレンジの最後まで保有する値
            'question_count': question_count,
            'correct_count': correct_count,
            'challenger': challenger,
            # -- 正解のチェックまで保有する値
            'question_id': question_id,
            # -- その他の値
            'question': question,
            'answer1': answer01,
            'answer2': answer02,
            'answer3': answer03,
        }, context_instance=RequestContext(request))

    else:
        massage = u'挑戦者名が設定されていません。'
        return HttpResponse(massage)

def check(request):
    if request.method == 'POST':
        # -- 挑戦者名
        challenger = request.POST.get('challenger', '')
        # -- 何問目か
        question_count = int(request.POST.get('question_count', ''))
        # -- 正解数
        correct_count = int(request.POST.get('correct_count', ''))
        # -- 入力された答
        input_answer = int(request.POST.get('input_answer', ''))
        # -- 問題の正解の取得       
        question_id = request.POST.get('question_id', '')
        record = Quiz.objects.get(pk=question_id)
        answer = int(record.answer)
        # -- 正解のチェック
        if input_answer == answer:
            correct_count = correct_count + 1
            message = u'すごい！正解です！！'
        else:
            message = u'ざんねん！不正解です！！'

        if question_count == 5:
            # -- ランキングデータの保存
            histry = Histry(name=challenger, points=correct_count)
            histry.save()
            return render_to_response('finish.html', {
                # -- チャレンジの最後まで保有する値
                'correct_count': correct_count,
                'challenger': challenger,
            }, context_instance=RequestContext(request))
        else:
            return render_to_response('check.html', {
                # -- チャレンジの最後まで保有する値
                'question_count': question_count,
                'correct_count': correct_count,
                'challenger': challenger,
                # -- 正解、不正解のメッセージ
                'message': message,
            }, context_instance=RequestContext(request))
    
    else:
        massage = u'挑戦者名が設定されていません。'
        return HttpResponse(massage)


def ranking(request):
    
    persons = Histry.objects.all().order_by('-points')[:10]

    return render_to_response('ranking.html', {
        'persons':persons,
    }, context_instance=RequestContext(request))
